package com.clive.controller;

import com.clive.bean.Dishes;
import com.clive.bean.DishesCategory;
import com.clive.service.DishesService;
import com.clive.service.DishesServiceImpl;
import com.clive.vo.DishResult;
import com.clive.vo.DishesVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@javax.servlet.annotation.WebServlet(name = "DishesController", urlPatterns = {"/dishes/addDishes"})
public class DishesController extends HttpServlet {
    private DishesService dishesService;
    private ObjectMapper mapper;

    //    方法的重写
    @Override
//	Servlet生命周期方法
    public void init() throws ServletException {
//		创建HttpServlet对象  只执行一次
        dishesService = new DishesServiceImpl();
        mapper = new ObjectMapper();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        用op区分唯一标识符
        String op = request.getParameter("op");
        if ("addDishes".equals(op)) {
//			接收页面传递过来的参数  根据key去value
            String dish_name = request.getParameter("dish_name");
            String c = request.getParameter("category_id");
//			类型转换
            Integer category_id = Integer.valueOf(c);
            String p = request.getParameter("price");
            Double price = Double.valueOf(p);
            System.out.println(dish_name + category_id + price);
//			创建一个对象把页面传过来的值放到对象里面
            Dishes dishes = new Dishes();
            dishes.setDishName(dish_name);
            DishesCategory category = new DishesCategory();
            category.setId(category_id);
            dishes.setPrice(price);
            dishes.setCategory(category);

//			将后台数据用json格式返回给页面 成功就是200 失败就是500或404
            String json = null;
            DishResult result = null;
//			controller接收页面请求,把数据放到对象里面去 调用service
            boolean b = dishesService.addDishes(dishes);
            if (b) {
                result = DishResult.ok(null);
                json = mapper.writeValueAsString(result);
            } else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }

            response.getWriter().print(json);
        } else if ("pageQuery".equals(op)) {
            Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
            Integer limit = Integer.valueOf(request.getParameter("limit"));
            DishesVo vo = dishesService.findDishesByCurrentPage(currentPage, limit);
            DishResult result = DishResult.ok(vo);
            String json = mapper.writeValueAsString(result);
            response.getWriter().print(json);
        } else if ("deleteDishes".equals(op)) {
            Integer dishesId = Integer.valueOf(request.getParameter("deleteId"));
            boolean b = dishesService.deleteDishesById(dishesId);
            String json = null;
            DishResult result = null;
            if (b) {
                result = DishResult.ok(null);
                json = mapper.writeValueAsString(result);
            } else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }
            response.getWriter().print(json);

        } else if ("findDishById".equals(op)) {
            Integer dishId = Integer.valueOf(request.getParameter("dishId"));
            Dishes dishes = dishesService.findDishesById(dishId);
            String json = mapper.writeValueAsString(DishResult.ok(dishes));
            response.getWriter().print(json);
        } else if ("updateDish".equals(op)) {
            Integer id = Integer.valueOf(request.getParameter("id"));
            String dish_name = request.getParameter("dish_name");
            Integer category_id = Integer.valueOf(request.getParameter("category_id"));
            Double price = Double.valueOf(request.getParameter("price"));

            Dishes dishes = new Dishes();
            dishes.setId(id);
            dishes.setDishName(dish_name);
            dishes.setPrice(price);
            DishesCategory category = new DishesCategory();
            category.setId(category_id);
            dishes.setCategory(category);

            boolean b = dishesService.updateDishesById(dishes);
            DishResult result = null;
            String json = null;
            if (b) {
                result = DishResult.ok(null);
                json = mapper.writeValueAsString(result);
            } else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }
            response.getWriter().print(json);
        }
    }
}
